<template>
  <div style="grid-column-start: 1;grid-column-end: 3;">
    <div class="info-head" style="padding-left: 0;">{{ title }}</div>
    <div class="info-content" style="padding-top: 20px;">
      <div class="flex justify-between overview">
        <div
          class="overview-item" v-for="(item, index) in info"
          :key="index" :style="{backgroundColor: themeColor}"
        >
          <h2 class="item-label">{{ item.label }}</h2>
          <div class="item-main">
            <span class="item-number">{{ item.count }}</span>
            <span class="item-unit">{{ item.unit }}</span>
          </div>
          <p class="item-content">
            {{ item.content }}
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  loadYearElectricityList, loadYearGasList, loadYearList,
  loadEnergyYearOverYear,
} from '@/api/energyDataManagement'
import { defaultNumber } from '@/utils'
import { COAL, OTHER, ENERGY_VARIETY } from '@/config'

export default {
  name: 'grid-item-one',
  props: {
    title: String,
  },
  data () {
    return {
      energyVariety: ENERGY_VARIETY,
      waterCount: '-',
      electricCount: '-',
      gasCount: '-',
      coalCount: '-',
      otherCount: '-',
      waterRatio: '-',
      electricRatio: '-',
      gasRatio: '-',
      coalRatio: '-',
      otherRatio: '-',
    }
  },
  computed: {
    themeColor () {
      return this.$store.state.user.theme
    },
    energyUnits () {
      return this.$store.state.energyUnits
    },
    info () {
      return this.energyVariety.map(v => ({
        label: v.name,
        count: this[v.value + 'Count'],
        unit: this.getUnit(v.value),
        content: '同比：' + this[v.value + 'Ratio'] + '%'
      }))
    },
  },
  mounted () {
    this.getInfo()
  },
  methods: {
    getUnit (key) {
      const energyUnits = this.energyUnits
      const target = energyUnits.find(v => v.key === key)
      return target ? target.value : ''
    },
    getInfo () {
      loadYearList(COAL).then(res => {
        if (res.code === 200) {
          res.data = res.data || {}
          this.coalCount = defaultNumber(res.data.val)
        }
      })
      loadYearList(OTHER).then(res => {
        if (res.code === 200) {
          res.data = res.data || {}
          this.otherCount = defaultNumber(res.data.val)
        }
      })
      // loadYearWaterList().then(res => {
      //   if (res.code === 200) {
      //     res.data = res.data || {}
      //     this.waterCount = formatNumber(res.data.val)
      //   }
      // })
      loadYearElectricityList().then(res => {
        if (res.code === 200) {
          res.data = res.data || {}
          this.electricCount = defaultNumber(res.data.val)
        }
      })
      loadYearGasList().then(res => {
        if (res.code === 200) {
          res.data = res.data || {}
          this.gasCount = defaultNumber(res.data.val)
        }
      })
      this.energyVariety.forEach(v => {
        loadEnergyYearOverYear(v.value).then(res => {
          if (res.code === 200) {
            res.data = res.data || {}
            this[v.value + 'Ratio'] = res.data.proportion || '-'
          }
        })
      })
    },
  },
}
</script>


<style lang="scss" scoped>
@import "@/assets/style/variable.scss";

.overview-item {
  flex: 1;
  border-radius: 4px;
  padding: 30px 20px;
  color: white;
  position: relative;
  overflow: hidden;

  & + .overview-item {
    margin-left: $gutter;
  }
}

.item-label {
  font-size: 22px;
  font-weight: 500;
  margin-top: 0;
  margin-bottom: 2rem;
}

.item-number {
  font-size: 20px;
  color: orange;
  margin-right: 5px;
}

.item-icon {
  position: absolute;
  right: 20px;
  bottom: 10px;
  font-size: 3vw;
  color: white;
  opacity: .8;
}
</style>
